package bb.lanxing.nav;

import com.baidu.mapapi.model.LatLng;

import java.util.List;

import bb.lanxing.model.json.RouteStep;
import bb.lanxing.util.PolyUtil;
import bb.lanxing.util.map.MathUtil;

public class NavDirectionHelper {
    private static final double DIRECTION_TOLERANCE = 40.0d;

    public static void calDirection(RouteStep routeStep, RouteStep routeStep2) {
        if (routeStep == null || routeStep2 == null) {
            return;
        }
        List<LatLng> polyPath = routeStep.getPolyPath();
        List<LatLng> polyPath2 = routeStep2.getPolyPath();
        if (polyPath == null || polyPath.size() < 2 || polyPath2 == null || polyPath2.size() < 2) {
            return;
        }
        List<LatLng> simplify = PolyUtil.simplify(polyPath, DIRECTION_TOLERANCE);
        handleDirection(routeStep2, calAngle(simplify.get(simplify.size() - 2), simplify.get(simplify.size() - 1), PolyUtil.simplify(polyPath2, DIRECTION_TOLERANCE).get(1)));
    }

    public static double calAngle(LatLng latLng, LatLng latLng2, LatLng latLng3) {
        double radians = Math.toRadians(latLng.longitude);
        double mercator = MathUtil.mercator(Math.toRadians(latLng.latitude));
        double radians2 = Math.toRadians(latLng2.longitude);
        double mercator2 = MathUtil.mercator(Math.toRadians(latLng2.latitude));
        double atan2 = Math.atan2(mercator2 - mercator, radians2 - radians) -
                Math.atan2(MathUtil.mercator(Math.toRadians(latLng3.latitude)) - mercator2,
                Math.toRadians(latLng3.longitude) - radians2);
        return atan2 > 3.141592653589793d ? atan2 - 6.283185307179586d : atan2 < -3.141592653589793d ? atan2 + 6.283185307179586d : atan2;
    }

    private static RouteStep handleDirection(RouteStep routeStep, double angle) {
        String maneuver = "";
        if (angle > 1.8325957145940461d) {
            maneuver = RouteStep.MANEUVER_SHARP_RIGHT;
        } else if (angle > 1.0471975511965976d) {
            maneuver = RouteStep.MANEUVER_RIGHT;
        } else if (angle > 0.2617993877991494d) {
            maneuver = RouteStep.MANEUVER_SLIGHT_RIGHT;
        } else if (angle > -0.2617993877991494d) {
            maneuver = RouteStep.MANEUVER_STRAIGHT;
        } else if (angle > -1.0471975511965976d) {
            maneuver = RouteStep.MANEUVER_SLIGHT_LEFT;
        } else if (angle > -1.8325957145940461d) {
            maneuver = RouteStep.MANEUVER_LEFT;
        } else {
            maneuver = RouteStep.MANEUVER_SHARP_LEFT;
        }
        routeStep.setManeuver(maneuver);
        return routeStep;
    }
}